/*
DocExtract
Version 1.0
Description: Controller class for VisualForce page GoogleDocs, contains methods for retreiving and sending 
document with GoogleDocs.
*/
public class DocExtract {
	
	private Boolean state = false;
	private String testString;

	public string authToken{ public get { 
        return ApexPages.currentPage().getParameters().get('sesstoken');
    } private set; }
    
	public string selectedSheet {get; set;}
	public String selectedClass {get; set;}  
	
	public Boolean getStatus(){
		return state;
	}
	  
	
		
	public List<String> getDocs(){
		List<String> options = new List<String>();
	  	if ( authToken == null ) return options;
	  	
	  	DocumentService service = new DocumentService();
	  	service.setAuthSubToken(this.authToken);
	  	GoogleData docList = service.getDocuments();
	  	
	  	String[] tokens;
        if(docList != null){
            for(xmldom.element entry: docList.entries){
                
                options.add(entry.getElementByTagName('title').nodeValue);
            }
        }
        return options;
	}
		
	public PageReference view(){
		state = true;
		return null;	
	}
	
	public List<SelectOption> getClassList(){
		List<SelectOption> options = new List<SelectOption>();
		
		ApexClass[] classes = [select Name, Id from ApexClass];
		for(ApexClass cls: classes){
			options.add(new SelectOption(cls.Id ,cls.Name));	
		}
		return options;
	}
	
	public PageReference upload(){
		
		
		DocumentService service = new DocumentService();
	  	service.setAuthSubToken(this.authToken);
		if(selectedClass!= null){
			ApexClass classToUpload = [select Name, Body from ApexClass where id=: selectedClass];
						
			GoogleData doc = service.uploadDocument(classToUpload.Body,classToUpload.Name,'text/plain'); 
			doc.dump();     
			if(doc.feed!= null){    
				ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.INFO, classToUpload.Name+'  Uploaded Successfully.');
       			ApexPages.addMessage(myMsg);
			}
		}
		else{
			ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.INFO,'Please select a Class from list!');
       		ApexPages.addMessage(myMsg);
		}
		return null;
	}
	
	public String getTest(){
		return testString;
	}	
}